import 'package:flutter/material.dart';
import 'package:labcloud_app/api/product/product_service.dart';
import 'package:labcloud_app/datas/product_ad.dart';

class ProductAdModel extends ChangeNotifier {
  List<ProductAD>? _productAdList = [];
  bool _isLoading = false;
  String? _errorMessage;

  List<ProductAD>? get productAdList => _productAdList;
  bool get isLoading => _isLoading;
  String? get errorMessage => _errorMessage;

  /// 加载广告列表数据
  Future<void> loadProductAds() async {
    if (_isLoading) return;

    _isLoading = true;
    _errorMessage = null;
    notifyListeners();

    try {
      // 这里需要根据实际API路径进行修改
      final response = await ProductApiService.getAdList();
      if (response.statusCode == 200) {
        if (response.data != null) {
          // print(response.data);
          response.data.forEach((item) {

            var value = ProductAD.fromJson(item);
            _productAdList?.add(value);
          });
          notifyListeners();
        }
      } else {
        _errorMessage = '加载失败，状态码: ${response.statusCode}';
      }
    } catch (e) {
      _errorMessage = '加载失败: $e';
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }

  /// 清除数据
  void clear() {
    _productAdList = null;
    _errorMessage = null;
    notifyListeners();
  }
}
